package com.ruoyi.plan.service.impl;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.plan.domain.PlanSubject;
import com.ruoyi.plan.domain.StudyContent;
import com.ruoyi.plan.mapper.PlanSubjectMapper;
import com.ruoyi.plan.service.IPlanSubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author MLYNDHG
 * @version 1.0
 * @description 计划科目Service业务层处理
 * @date 2023/12/4 16:49
 */

@Service
public class PlanSubjectServiceImpl implements IPlanSubjectService
{
  @Autowired
  private PlanSubjectMapper planSubjectMapper;

  /**
   * 查询计划科目
   *
   * @param id 计划科目主键
   * @return 计划科目
   */
  @Override
  public PlanSubject selectPlanSubjectById(Long id)
  {
    return planSubjectMapper.selectPlanSubjectById(id);
  }

  /**
   * 查询计划科目列表
   *
   * @param planSubject 计划科目
   * @return 计划科目
   */
  @Override
  public List<PlanSubject> selectPlanSubjectList(PlanSubject planSubject)
  {
    planSubject.setUid(SecurityUtils.getUserId());
    return planSubjectMapper.selectPlanSubjectList(planSubject);
  }

  /**
   * 新增计划科目
   *
   * @param planSubject 计划科目
   * @return 结果
   */
  @Transactional
  @Override
  public int insertPlanSubject(PlanSubject planSubject)
  {
    planSubject.setUid(SecurityUtils.getUserId());
    // 创建一个java.util.Date对象，表示当前日期和时间
    Date date = new Date();
    // 创建一个java.sql.Date对象，表示当前日期
    Date sqldate = new Date(date.getTime());
    planSubject.setCreateDate(sqldate);
    int rows = planSubjectMapper.insertPlanSubject(planSubject);
    insertStudyContent(planSubject);
    return rows;
  }

  /**
   * 修改计划科目
   *
   * @param planSubject 计划科目
   * @return 结果
   */
  @Transactional
  @Override
  public int updatePlanSubject(PlanSubject planSubject)
  {
    planSubjectMapper.deleteStudyContentBySubjectId(planSubject.getId());
    insertStudyContent(planSubject);
    return planSubjectMapper.updatePlanSubject(planSubject);
  }

  /**
   * 批量删除计划科目
   *
   * @param ids 需要删除的计划科目主键
   * @return 结果
   */
  @Transactional
  @Override
  public int deletePlanSubjectByIds(Long[] ids)
  {
    planSubjectMapper.deleteStudyContentBySubjectIds(ids);
    return planSubjectMapper.deletePlanSubjectByIds(ids);
  }

  /**
   * 删除计划科目信息
   *
   * @param id 计划科目主键
   * @return 结果
   */
  @Transactional
  @Override
  public int deletePlanSubjectById(Long id)
  {
    planSubjectMapper.deleteStudyContentBySubjectId(id);
    return planSubjectMapper.deletePlanSubjectById(id);
  }

  /**
   * 新增学习内容信息
   *
   * @param planSubject 计划科目对象
   */
  public void insertStudyContent(PlanSubject planSubject)
  {
    List<StudyContent> studyContentList = planSubject.getStudyContentList();
    Long id = planSubject.getId();
    if (StringUtils.isNotNull(studyContentList))
    {
      List<StudyContent> list = new ArrayList<StudyContent>();
      for (StudyContent studyContent : studyContentList)
      {
        studyContent.setSubjectId(id);
        list.add(studyContent);
      }
      if (list.size() > 0)
      {
        planSubjectMapper.batchStudyContent(list);
      }
    }
  }
}